#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdlib>

int main() {
    pid_t pid = fork();

    if (pid < 0) {
        std::cerr << "Fork failed!" << std::endl;
        return 1;
    } else if (pid == 0) {
        // Child process
        std::cout << "Child process running, PID: " << getpid() << std::endl;
        exit(1); // Exit normally
    } else {
        // Parent process
        int status = 0;
        waitpid(pid,&status,0); // Wait for the child process to terminate
        std::cout<<status<<std::endl; // 退出码是1的时候,status的值就是256

        if (WIFEXITED(status)) {
            std::cout << "Child exited with status: " << WEXITSTATUS(status) << std::endl;
            // 显示退出信号:
            std::cout << "Child exited with signal: " << WTERMSIG(status) << std::endl;
        } else {
            std::cout << "Child did not exit normally." << std::endl;
        }
    }

    return 0;
}